package com.tkjt.pms.modules.indexvaluemirror.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.common.entity.TkIndexValueMirror;
import com.tkjt.pms.common.utils.DepartCode;
import com.tkjt.pms.modules.common.mapper.TkDataConfigMapper;
import com.tkjt.pms.modules.common.service.TkDataConfigService;
import com.tkjt.pms.modules.indexvaluemirror.mapper.IndexValueMirrorMapper;
import com.tkjt.pms.modules.indexvaluemirror.service.IndexValueMirrorService;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("indexValueMirrorService")
@Transactional
public class IndexValueMirrorServiceImpl implements IndexValueMirrorService {
    @Autowired
    private IndexValueMirrorMapper indexValueMirrorMapper;
    @Autowired
    private TkDataConfigService tkDataConfigService;
    @Override
    public Result<?> datagrid(LoginUser loginUser, TkIndexValueMirror tkIndexValueMirror, Integer pageNo, Integer pageSize, HttpServletRequest req) {
        String dataType = tkDataConfigService.getDataType(loginUser.getUsername());
        Map<String,Object> queryIndexValue = new HashMap<>();
        int count = 0;
        queryIndexValue.put("min",(pageNo-1)*pageSize);
        queryIndexValue.put("max",pageNo*pageSize);
        queryIndexValue.put("indexDepart",tkIndexValueMirror.getIndexDepart());
        queryIndexValue.put("dataType",dataType);
        queryIndexValue.put("indexExecutorDepart",tkIndexValueMirror.getIndexExecutorDepart());
        List<TkIndexValueMirror> list = indexValueMirrorMapper.selectPageListPyParam(queryIndexValue);
        count = indexValueMirrorMapper.selectPageCountPyParam(queryIndexValue);
        for(int i = 0 ; i < list.size() ; i++) {
            list.get(i).setMTotal(list.get(i).getMTotal() == 0 ? null : DepartCode.formateForDouble(list.get(i).getMTotal()));
            list.get(i).setM01(list.get(i).getM01() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM01()));
            list.get(i).setM02(list.get(i).getM02() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM02()));
            list.get(i).setM03(list.get(i).getM03() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM03()));
            list.get(i).setM04(list.get(i).getM04() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM04()));
            list.get(i).setM05(list.get(i).getM05() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM05()));
            list.get(i).setM06(list.get(i).getM06() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM06()));
            list.get(i).setM07(list.get(i).getM07() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM07()));
            list.get(i).setM08(list.get(i).getM08() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM08()));
            list.get(i).setM09(list.get(i).getM09() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM09()));
            list.get(i).setM10(list.get(i).getM10() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM10()));
            list.get(i).setM11(list.get(i).getM11() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM11()));
            list.get(i).setM12(list.get(i).getM12() == 0 ? null : DepartCode.formateForDouble(list.get(i).getM12()));
        }
        Page<TkIndexValueMirror> finalPage = new Page<TkIndexValueMirror>(pageNo, pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count/pageSize;
        if(count%pageSize > 0){
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(list);
        return Result.ok(finalPage);
    }
}
